package cn.iocoder.yudao.module.system.controller.open.account;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import cn.iocoder.yudao.module.system.controller.open.account.vo.DeductFeeReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.system.enums.product.ProductEnum;
import cn.iocoder.yudao.module.system.service.tenant.TenantAccountService;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "OPEN-API - 账户管理")
@RestController
@RequestMapping("/system/account")
@Validated
@Slf4j
public class OpenAccountController {

    @Resource
    private TenantService tenantService;


    @PostMapping("/ai-deduct-fee")
    @Operation(summary = "AI外呼扣费")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<Long> aiDeductFee(@RequestBody @Valid DeductFeeReqVO reqVO) {
        Long release = tenantService.updateTenantProductBalance(ProductEnum.ROBOT_CALL.getCode(), reqVO.getTenantId(), false, reqVO.getFee());
        return success(release);
    }

    @GetMapping("/get-ai-balance")
    @Operation(summary = "获取ai外呼余额")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<Integer> getAiBalance(@RequestParam("tenantId") Long tenantId) {
        Integer release = tenantService.getTenantProductBalance(ProductEnum.ROBOT_CALL.getCode(), tenantId);
        return success(release);
    }
}
